package com.vimi8.ebb.configuration;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.util.StringUtils;
import redis.clients.jedis.JedisShardInfo;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

import java.lang.reflect.Method;

@javax.annotation.Generated(value = "class io.swagger.codegen.languages.SpringCodegen", date = "2016-09-07T03:24:42.579Z")

@Configuration
@EnableCaching
public class SwaggerDocumentationConfig extends CachingConfigurerSupport  {
    private Logger logger =  LoggerFactory.getLogger(this.getClass());
    @Value("${spring.redis.host}")
    private String host;
// org.springframework.jdbc.datasource.DriverManagerDataSource
    @Bean
    JedisConnectionFactory jedisConnectionFactory() {
        JedisShardInfo jedisShardInfo=new JedisShardInfo(host);
        JedisConnectionFactory jedisConnectionFactory= new JedisConnectionFactory(jedisShardInfo);
        logger.info("connector is closed "+jedisConnectionFactory.getConnection().isClosed());
        return jedisConnectionFactory;
    }

    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
        RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
        template.setConnectionFactory(jedisConnectionFactory());
        template.setKeySerializer(new StringRedisSerializer());
        template.setValueSerializer(new RedisObjectSerializer());
        return template;
    }
    @Bean(name = "keyGenerator")
    public KeyGenerator wiselyKeyGenerator() {
        return new KeyGenerator() {
            @Override
            public Object generate(Object target, Method method, Object... params) {
                StringBuilder sb = new StringBuilder();
                sb.append(target.getClass().getName());
                sb.append(method.getName());
                if(StringUtils.isEmpty(params)){
                    return sb.toString();
                }
                for (Object obj : params) {
                    if(!StringUtils.isEmpty(obj)){
                        sb.append(obj.toString());
                    }

                }
                logger.info("keyGenerator=="+sb.toString());
                return sb.toString();
            }
        };
    }

    @SuppressWarnings("rawtypes")
    @Bean
    public CacheManager cacheManager(RedisTemplate redisTemplate) {
        RedisCacheManager rcm = new RedisCacheManager(redisTemplate);
        //设置缓存过期时间
        rcm.setDefaultExpiration(3600*12);//秒
        return rcm;
    }
    ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("info-service")
                .description("信息服务")
                .license("")
                .licenseUrl("")
                .termsOfServiceUrl("")
                .version("1.1.6")
                .contact(new Contact("","", ""))
                .build();
    }

    @Bean
    public Docket customImplementation(){
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.vimi8.ebb.api"))
                .build()
                .directModelSubstitute(org.joda.time.LocalDate.class, java.sql.Date.class)
                .directModelSubstitute(org.joda.time.DateTime.class, java.util.Date.class)
                .apiInfo(apiInfo());
    }


}
